package gdupt;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import gdupt.utils.Calculate;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 程序的控制器
 *
 * @author zhn
 */
public class CalculatorController implements Initializable {

    String result = null;
    @FXML
    private TextArea txt_suffix;

    @FXML
    private TextArea txt_write_in;

    @FXML
    private TextArea txt_answer;


    @FXML
    private Button cancel;

    @FXML
    private Button reduce;

    @FXML
    private Button add;

    @FXML
    private Button six;

    @FXML
    private Button night;

    @FXML
    private Button one;

    @FXML
    private Button clear;

    @FXML
    private Button seven;

    @FXML
    private Button suffix;

    @FXML
    private Button two;

    @FXML
    private Button three;

    @FXML
    private Button eight;

    @FXML
    private Button four;

    @FXML
    private Button logo;

    @FXML
    private Button divide;

    @FXML
    private Button multiply;

    @FXML
    private Button five;

    @FXML
    void logo_action(ActionEvent event) {
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("版本的信息");
        alert.setContentText("开发者:zhn/xwt");
        alert.show();
    }

    @FXML
    void clear_action(ActionEvent event) {
        txt_write_in.clear();
        txt_answer.clear();
        txt_suffix.clear();
    }

    @FXML
    void cancel_action(ActionEvent event) {
        try {
            txt_write_in.setText(txt_write_in.getText().substring(0, txt_write_in.getText().length() - 1));
        } catch (Exception e) {

        }
    }


    @FXML
    void divide_action(ActionEvent event) {

        txt_write_in.appendText("/");
    }

    @FXML
    void seven_action(ActionEvent event) {

        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void eight_action(ActionEvent event) {

        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void night_action(ActionEvent event) {

        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void multiply_action(ActionEvent event) {

        txt_write_in.appendText("*");
    }

    @FXML
    void four_action(ActionEvent event) {

        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void five_action(ActionEvent event) {

        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void six_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void reduce_action(ActionEvent event) {

        txt_write_in.appendText("-");
    }

    @FXML
    void one_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void two_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));

    }

    @FXML
    void three_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));

    }

    @FXML
    void add_action(ActionEvent event) {
        txt_write_in.appendText("+");

    }

    @FXML
    void suffix_action(ActionEvent event) {
        Alert alert = new Alert(AlertType.INFORMATION);
        if (result == null) {
            alert.setTitle("计算错误");
            alert.setContentText("未输入计算的表达式");
            alert.show();
        } else {
            alert.setTitle("显示表达式");
            alert.setContentText("后缀表达式是将结果是:" + result);
            alert.show();
        }

    }

    @FXML
    void zero_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void point_action(ActionEvent event) {
        txt_write_in.appendText((((Button) event.getSource()).getText()));
    }

    @FXML
    void equal_action(ActionEvent event) {
        System.out.println(txt_write_in.getText());
        //实例化计算类
        Calculate calculate = new Calculate();

        try {
            //将中缀表达式转化为后缀表达式
            String[] hzExpression = calculate.getHZExpression(txt_write_in.getText());
            //实例化字符串缓冲区,拼接字符串
            StringBuilder sb = new StringBuilder();
            //循环遍历数组,将后缀表达式保存到字符缓冲区中去
            for (String s : hzExpression) {
                sb.append(s + " ");
            }
            //显示后缀表达式
            txt_suffix.setText(sb.toString());
            //接受后缀表达式结果
            result = sb.toString();
            //显示计算返回的结构
            txt_answer.setText(calculate.HZOperation(hzExpression).toString());
        } catch (Exception e) {
            Alert alert = new Alert(AlertType.WARNING);
            alert.setTitle("计算错误");
            alert.setContentText("输入的表达式暂不支持计算");
            alert.show();
        }
    }

    private List op = new ArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        op.add("*");
        op.add("/");
        op.add("+");
        op.add("-");
        op.add("(");
        op.add(")");
        //监听文本域中值的变化

        //监听文本域中值的变化
        txt_write_in.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                //获取倒数第二个字符
                String x = null;
                //获取倒数第一个字符
                String y = null;
                try {
                    x = txt_write_in.getText().substring(txt_write_in.getText().length() - 2, txt_write_in.getText().length() - 1);
                    y = txt_write_in.getText().substring(txt_write_in.getText().length() - 1);
                } catch (Exception e) {

                }
                //如果倒数第二字符和倒数第一字符均是计算符号,则将倒数第二字符删除,达到替换的效果
                if (op.contains(y)) {
                    if (op.contains(x)) {
                        txt_write_in.setText(txt_write_in.getText().substring(0, txt_write_in.getText().length() - 2) + txt_write_in.getText().substring(txt_write_in.getText().length() - 1));
                    }
                }
            }
        });

    }
}
